package com.supinfo.sun.sungamers.dao.impl;

import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.supinfo.sun.sungamers.dao.PersonDAO;
import com.supinfo.sun.sungamers.person.Person;

/**
 * @author Liu, Bing(Locke)
 * @since 2011-05-05
 * @version 0.3
 */
public class PersonDB implements PersonDAO {

	private Connection conn;
	
	public PersonDB(Connection conn) {
		this.conn = conn;
	}
	
	public List<Person> getPersons() {
		// TODO Auto-generated method stub
		return null;
	}

	public void savePersons(List<Person> persons) {
		// TODO Auto-generated method stub
		
	}

	public void appendPerson(Person p) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String birthDate = dateFormat.format(p.getBrithDate());
		String addPersonSQL = "INSERT INTO persons(id, firstname, lastname, birthdate) VALUES("
			+ p.getId()
			+ ",'"
			+ p.getFirstName()
			+ "','"
			+ p.getLastName()
			+ "','"
			+ birthDate
			+ "');";
		try {
			Statement stmt = conn.createStatement();
			stmt.execute(addPersonSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Person getPerson(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public void updatePerson(Person p) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String birthDate = dateFormat.format(p.getBrithDate());
		String updatePersonSQL = "UPDATE persons SET firstname='" 
			+ p.getFirstName()
			+ "',lastname='"
			+ p.getLastName()
			+ "', birthdate='"
			+ birthDate
			+ "' WHERE id="
			+ p.getId()
			+ ";";
		try {
			Statement stmt = conn.createStatement();
			stmt.execute(updatePersonSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deletePerson(Person p) {
		String delPersonSQL = "DELETE FROM persons WHERE id=" 
			+ p.getId()
			+ ";";
		try {
			Statement stmt = conn.createStatement();
			stmt.execute(delPersonSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}

}
